package cn.lanqiao.service.impl;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import cn.lanqiao.service.MailService;
import cn.lanqiao.util.CacheMap;
import cn.lanqiao.util.EmailCodeUtils;

@Service("mailService")

public class MailServiceImpl implements MailService {

	@Autowired
	private JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")
	private String mailName;

	@Override
	public void sendSimpleMail(String to, String title, String content) {
		
		
	}

	@Override
	public void sendAttachmentsMail(String to, String title, String cotent, List<File> fileList) {
		
		
	}

	@Override
	public String sendMailCode(String mail, HttpServletRequest request) {
		String code = EmailCodeUtils.getNumber();
		try {
			SimpleMailMessage message = new SimpleMailMessage();
			message.setSubject("注册验证码");
			message.setText("注册验证码是：" + code);
			message.setTo(mail);
			message.setFrom(mailName);
			javaMailSender.send(message);
		}catch (Exception e){
			System.out.println(e.getMessage());
			return "发送失败, 请确认邮箱是否正确!";
		}		
		CacheMap.CACHE_CODE_MAP.put(mail, code);
		return "发送成功!";

	}


}
